package com.wjv.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>Title: OrderController</p>
 * <p>Company:misspao </p>
 *
 * @author: wjv
 * @Date: Create in 11:27 2020/10/10
 * @Description:
 */
@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping(value = "/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id) {
        return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class);
    }

    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }

    /**
     * 程序的异常 走 fallback
     * 限流的异常 走 blockHandler
     * value 是 必须的
     * @param p1
     * @param p2
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "dealTestHotKey", fallback = "fallback")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        if ("3".equals(p1)) {
            int a = 10 / 0;
        }
        return "------testHotKey";
    }

    /**
     * 参数、返回类型 ，要和目标方法一致
     * 可以添加 BlockException 参数
     * @param p1
     * @param p2
     * @param exception
     * @return
     */
    public String dealTestHotKey(String p1, String p2, BlockException exception) {
        return "------dealTestHotKey,o(╥﹏╥)o";
    }

    /**
     * 参数、返回类型 ，要和目标方法一致
     * 可以添加 Throwable 参数
     * @param p1
     * @param p2
     * @param exception
     * @return
     */
    public String fallback(String p1, String p2, Throwable exception) {
        System.out.println(exception);
        return "fallback, o(╥﹏╥)o";
    }


}
